package com.stock;

import com.kevin.core.test.AbstractDaoTest;
import com.kevin.core.util.DateUtils;
import com.kevin.dao.DayKDao;
import com.kevin.entity.DayK;
import com.kevin.entity.Stock;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Date;
import java.util.List;

public class Main extends AbstractDaoTest {

    @Autowired
    DayKDao dao;

    @Autowired
    StockDetailService stockDetailService;

    @Autowired
    StockService stockService;

    @Autowired
    DayKService daykService;



    @Test
    public void saveStockList() {
        System.out.println("save stock list begin...");
        stockService.deleteAll();
        stockService.save("zxb");
        System.out.println("save stock list completed.");
    }

    @Test
    public void saveStockDetailAll() {
        System.out.println("save stockDetail  begin...");
        StockService stockService = new StockService();
        List<Stock> stockList = stockService.queryList("zxb");
        if (stockList.isEmpty()) {
            System.err.println("stock list size:0");
            return;
        } else {
            stockDetailService.deleteAll();
        }
        System.out.println("stock list size:" + stockList.size());
        int i = 0;
        for (Stock stock : stockList) {
            stockDetailService.save(stock.getSymbol());
            i += 1;
            System.out.println("[" + stockList.size() + "] " + i + " completed");
        }
        System.out.println("save stockDetail completed.");
    }


    @Test
    public void saveDayKAll() {
        System.out.println("save all dayk begin...");
        List<Stock> stockList = stockService.queryList("zxb");
        if (stockList.isEmpty()) {
            System.err.println("stock list is empty");
            return;
        } else {
            dao.deleteAll();
        }
        System.out.println("stock list size:" + stockList.size());
        Date _beginDate= DateUtils.getPrevious(5);
        Date _endDate=new Date();
        String beginDate= DateUtils.formatDate(_beginDate,DateUtils.DATE_FORMAT);
        String endDate= DateUtils.formatDate(_endDate,DateUtils.DATE_FORMAT);
        int i = 0;
        for (Stock stock : stockList) {
            daykService.save(stock.getSymbol(), beginDate, endDate);
            i += 1;
            System.out.println("[" + stockList.size() + "] " + i + " completed");
        }
        System.out.println("save all dayk completed.");
    }


    //@Test
    public void saveDayKone() {
        String symbol = "SZ002657";
        Logger.info("save {} dayk begin...", symbol);
        daykService.save(symbol, "2015-01-01", "2015-12-01");
        Logger.info("save {} dayk completed.", symbol);
    }





    //	@Test
    public void analyze() {
        String symbol = "SZ002657";
        System.out.println("analyze begin...");
        List<DayK> daykList = dao.findBySymbol(symbol);
        for (int i = 0; i < daykList.size() - 1; i++) {
            DayK cur = daykList.get(i);
            DayK next = daykList.get(i + 1);
            next.setLossVolume(cur.getVolume() - next.getVolume());
        }
        Collections.sort(daykList);
        for (DayK k : daykList) {
            System.out.println(k);
        }
        System.out.println("analyze completed.");
    }


    /**
     * 股票年化利率
     */
    @Test
    public void stock_year1(){
        double step=0.0046;//缩量，1个点出，平均每日涨幅
        double result=1;
        for(int i=0;i<212;i++){
            result=result*(1+0.0046);
        }
        System.out.println(result);
    }

    /**
     * 股票年化利率
     */
    @Test
    public void stock_year2(){
        double step=0.0046;//缩量，1个点出，平均每日涨幅
        double result=1;
        for(int i=0;i<212;i++){
            result=result*(1+0.0046);
        }
        System.out.println(result);
    }

}
